package com.caregiver.watch.common.helper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.function.Supplier;

/**
 * @description: 事务工具类
 * @program: caregiver-watch
 * @author: 疆戟
 * @create: 2025-10-06 11:25
 */
@Component
@Slf4j
public class TransactionalHelper {

    @Transactional(rollbackFor = Exception.class)
    public <T> T execute(Supplier<T> supplier) {
        try {
            return supplier.get();
        } catch (Exception e) {
            log.error("事务执行异常", e);
            throw e;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void execute(Runnable runnable) {
        try {
            runnable.run();
        } catch (Exception e) {
            log.error("事务执行异常", e);
            throw e;
        }
    }
}
